package org.shop.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.shop.dao.Impl.ProductDaoImpl;
import org.shop.dao.ProductDao;
import org.shop.entity.Product;
import org.shop.vo.ResponseData;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ZJL
 * @date 2024-12-11
 */
@WebServlet("/updateStatus.do")
public class UpdateProductStatusServlet extends BaseController {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String productId = req.getParameter("productId");
        String status = req.getParameter("status");

        //实例化 DAO
        ProductDao productDao = new ProductDaoImpl();
        //调用 DAO 对象的方法
        //根据商品ID找到需要修改上架状态的商品
        Product p = productDao.selectById(productId);
            p.setProductStatus(Integer.parseInt(status));
            int update = productDao.update(p);
            if (update > 0) {
                if (status.equals("1")) {
                    print(resp,success(200,null,"上架成功！"));
                }else {
                    print(resp,success(200,null,"下架成功！"));
                }
            }else{
                if (status.equals("1")) {
                    print(resp,error(500,"上架失败！"));
                }else {
                    print(resp,error(500,"下架失败！"));
                }
            }

    }
}
